package algorithm.unionfind;

public class UnionFind2 extends AbstractUnionFind {


    public UnionFind2(int capacity) {
        super(capacity);
    }


    @Override
    public int find(int p) {
        while (p != data[p]) {
            p = data[p];
        }
        return p;
    }



    @Override
    public void union(int p, int q) {
        int pRoot = find(p);
        int qRoot = find(q);

        if (pRoot == qRoot) {
            return;
        }
        data[pRoot] = qRoot;
    }
}
